#ifndef __PCPWM_H
#define __PCPWM_H

#include <p18cxxx.h>
#include <pconfig.h>

#if defined (PCPWM_V1) || defined (PCPWM_V2) || defined (PCPWM_V3)

#define PCPWM_TMR_INT_EN 	{  \
							PIR3bits.PTIF = 0; \
							PIE3bits.PTIE = 1;}

#define PCPWM_TMR_INT_DIS {PIE3bits.PTIE = 0;}

#endif

//*************************************************************
#define PCPWM_TMR_INT_EN 	{  \
							PIR3bits.PTIF = 0; \
							PIE3bits.PTIE = 1;}

#define PCPWM_TMR_INT_DIS {PIE3bits.PTIE = 0;}
//**************************************************************
//---------pcpwm.h----------------------

// DTCON register
// DTCON<7:6> 
#ifdef USE_OR_MASKS
#define DT_CLK_SRC_FOSC_16		0b11000000		
#define DT_CLK_SRC_FOSC_8	    0b10000000
#define DT_CLK_SRC_FOSC_4		0b01000000
#define DT_CLK_SRC_FOSC_2		0b00000000
#define DT_CLK_SRC_MASK			(~DT_CLK_SRC_FOSC_16)
#else
#define DT_CLK_SRC_FOSC_16		0b11111111		
#define DT_CLK_SRC_FOSC_8	    0b10111111
#define DT_CLK_SRC_FOSC_4		0b01111111
#define DT_CLK_SRC_FOSC_2		0b00111111

#endif

// PTCON0 register
// PTCON0<7:4>
#ifdef USE_OR_MASKS
#define PT_POS_1_1				0b00000000 
#define PT_POS_1_2				0b00010000
#define PT_POS_1_3				0b00100000
#define PT_POS_1_4				0b00110000
#define PT_POS_1_5				0b01000000
#define PT_POS_1_6				0b01010000
#define PT_POS_1_7				0b01100000
#define PT_POS_1_8				0b01110000
#define PT_POS_1_9				0b10000000
#define PT_POS_1_10				0b10010000
#define PT_POS_1_11				0b10100000
#define PT_POS_1_12				0b10110000
#define PT_POS_1_13				0b11000000
#define PT_POS_1_14				0b11010000
#define PT_POS_1_15				0b11100000
#define PT_POS_1_16				0b11110000
#define PT_POS_MASK				(~PT_POS_1_16)
#else 
#define PT_POS_1_1				0b00001111 
#define PT_POS_1_2				0b00011111
#define PT_POS_1_3				0b00101111
#define PT_POS_1_4				0b00111111
#define PT_POS_1_5				0b01001111
#define PT_POS_1_6				0b01011111
#define PT_POS_1_7				0b01101111
#define PT_POS_1_8				0b01111111
#define PT_POS_1_9				0b10001111
#define PT_POS_1_10				0b10011111
#define PT_POS_1_11				0b10101111
#define PT_POS_1_12				0b10111111
#define PT_POS_1_13				0b11001111
#define PT_POS_1_14				0b11011111
#define PT_POS_1_15				0b11101111
#define PT_POS_1_16				0b11111111


#endif

// PTCON0<3:2>
#ifdef USE_OR_MASKS
#define PT_PRS_1_1				0b00000000
#define PT_PRS_1_4				0b00000100
#define PT_PRS_1_16				0b00001000
#define PT_PRS_1_64				0b00001100
#define	PT_PRS_MASK				(~PT_PRS_1_64)
#else
#define PT_PRS_1_1				0b11110011
#define PT_PRS_1_4				0b11110111
#define PT_PRS_1_16				0b11111011
#define PT_PRS_1_64				0b11111111
#endif


// PTCON0<1:0>
#ifdef USE_OR_MASKS
#define PT_MOD_CNT_UPDN_INT		0b00000011
#define PT_MOD_CNT_UPDN			0b00000010
#define PT_MOD_SNGL_SHOT		0b00000001
#define PT_MOD_FREE_RUN			0b00000000
#define	PT_MOD_MASK				(~PT_MOD_CNT_UPDN_INT)
#else
#define PT_MOD_CNT_UPDN_INT		0b11111111
#define PT_MOD_CNT_UPDN			0b11111110
#define PT_MOD_SNGL_SHOT		0b11111101
#define PT_MOD_FREE_RUN			0b11111100

#endif

// PTCON1 register
// PTCON1<7>
#ifdef USE_OR_MASKS
#define PT_ENABLE				0b10000000	
#define PT_DISABLE				0b00000000	
#define PT_MASK					(~PT_ENABLE)
#else
#define PT_ENABLE				0b11111111	
#define PT_DISABLE				0b01111111	
#endif


// PTCON1<6>
#ifdef USE_OR_MASKS
#define PT_CNT_UP				0b00000000	
#define PT_CNT_DWN				0b01000000	
#define PT_CNT_MASK				(~PT_CNT_DWN)
#else
#define PT_CNT_UP				0b10111111	
#define PT_CNT_DWN				0b11111111	

#endif


// PWMCON0 register
// PWMCON0<6:4>

#ifdef USE_OR_MASKS
#define PWM_IO_ALL_ODD			0b01110000
#define PWM_IO_1AND3			0b01100000
#define PWM_IO_ALL				0b01010000
#define PWM_IO_0TO5				0b01000000
#define PWM_IO_0TO3				0b00110000
#define PWM_IO_0AND1			0b00100000
#define PWM_IO_1				0b00010000
#define PWM_DISABLE				0b00000000
#define	PWM_IO_MASK				(~PWM_IO_ALL_ODD)
#else
#define PWM_IO_ALL_ODD			0b01111111
#define PWM_IO_1AND3			0b01101111
#define PWM_IO_ALL				0b01011111
#define PWM_IO_0TO5				0b01001111
#define PWM_IO_0TO3				0b00111111
#define PWM_IO_0AND1			0b00101111
#define PWM_IO_1				0b00011111
#define PWM_DISABLE				0b00001111
#endif

// PWMCON0<3:0>

#ifdef USE_OR_MASKS
#define PWM_0AND1_INDPEN		0b00000001			//INDEPENDENT
#define PWM_0AND1_COMPLI		0b00000000
#define PWM_0AND1_MASK			(~PWM_0AND1_INDPEN)
#else 
#define PWM_0AND1_INDPEN		0b11111111			//INDEPENDENT
#define PWM_0AND1_COMPLI		0b11111110
#endif

#ifdef USE_OR_MASKS
#define PWM_2AND3_INDPEN		0b00000010			//INDEPENDENT
#define PWM_2AND3_COMPLI		0b00000000
#define PWM_2AND3_MASK			(~PWM_2AND3_INDPEN)
#else 
#define PWM_2AND3_INDPEN		0b11111111			//INDEPENDENT
#define PWM_2AND3_COMPLI		0b11111101
#endif

#ifdef USE_OR_MASKS
#define PWM_4AND5_INDPEN		0b00000100			//INDEPENDENT
#define PWM_4AND5_COMPLI		0b00000000
#define PWM_4AND5_MASK			(~PWM_4AND5_INDPEN)
#else 
#define PWM_4AND5_INDPEN		0b11111111			//INDEPENDENT
#define PWM_4AND5_COMPLI		0b11111011
#endif


#if defined (PCPWM_V1) || defined (PCPWM_V2)
#ifdef USE_OR_MASKS
#define PWM_6AND7_INDPEN		0b00001000			//INDEPENDENT
#define PWM_6AND7_COMPLI		0b00000000
#define PWM_6AND7_MASK			(~PWM_6AND7_INDPEN)
#else 
#define PWM_6AND7_INDPEN		0b11111111			//INDEPENDENT
#define PWM_6AND7_COMPLI		0b11110111
#endif
#endif

// PWMCON1 register
// PWMCON1<7:4>
#ifdef USE_OR_MASKS
#define PW_SEVT_POS_1_1				0b00000000 
#define PW_SEVT_POS_1_2				0b00010000
#define PW_SEVT_POS_1_3				0b00100000
#define PW_SEVT_POS_1_4				0b00110000
#define PW_SEVT_POS_1_5				0b01000000
#define PW_SEVT_POS_1_6				0b01010000
#define PW_SEVT_POS_1_7				0b01100000
#define PW_SEVT_POS_1_8				0b01110000
#define PW_SEVT_POS_1_9				0b10000000
#define PW_SEVT_POS_1_10			0b10010000
#define PW_SEVT_POS_1_11			0b10100000
#define PW_SEVT_POS_1_12			0b10110000
#define PW_SEVT_POS_1_13			0b11000000
#define PW_SEVT_POS_1_14			0b11010000
#define PW_SEVT_POS_1_15			0b11100000
#define PW_SEVT_POS_1_16			0b11110000
#define PW_SEVT_POS_MASK			(~PW_SEVT_POS_1_16)
#else
#define PW_SEVT_POS_1_1				0b00001111 
#define PW_SEVT_POS_1_2				0b00011111
#define PW_SEVT_POS_1_3				0b00101111
#define PW_SEVT_POS_1_4				0b00111111
#define PW_SEVT_POS_1_5				0b01001111
#define PW_SEVT_POS_1_6				0b01011111
#define PW_SEVT_POS_1_7				0b01101111
#define PW_SEVT_POS_1_8				0b01111111
#define PW_SEVT_POS_1_9				0b10001111
#define PW_SEVT_POS_1_10			0b10011111
#define PW_SEVT_POS_1_11			0b10101111
#define PW_SEVT_POS_1_12			0b10111111
#define PW_SEVT_POS_1_13			0b11001111
#define PW_SEVT_POS_1_14			0b11011111
#define PW_SEVT_POS_1_15			0b11101111
#define PW_SEVT_POS_1_16			0b11111111
#endif


// PWMCON1<3>
#ifdef USE_OR_MASKS
#define PW_SEVT_DIR_UP				0b00000000
#define PW_SEVT_DIR_DWN				0b00001000
#define PW_SEVT_DIR_MASK			(~PW_SEVT_DIR_DWN)
#else
#define PW_SEVT_DIR_UP				0b11110111
#define PW_SEVT_DIR_DWN				0b11111111
#endif

// PWMCON1<0>
#ifdef USE_OR_MASKS
#define PW_OP_SYNC					0b00000001
#define PW_OP_ASYNC					0b00000000
#define PW_OP_SYNC_MASK				(~PW_OP_SYNC)			
#else 
#define PW_OP_SYNC					0b11111111
#define PW_OP_ASYNC					0b11111110
#endif

// FLTCONFIG
// FLTCONFIG<7>
#define BRK_FLT_EN					FLTCONFIGbits.BRFEN = 1;	
#define BRK_FLT_DIS					FLTCONFIGbits.BRFEN = 0;


#if defined (PCPWM_V1) || defined (PCPWM_V2)
// FLTCONFIG<5>
#define FLT_B_CY_CY					FLTCONFIGbits.FLTBMOD = 1;	
#define FLT_B_CATAS					FLTCONFIGbits.FLTBMOD = 0;	

// FLTCONFIG<4>
#define FLT_B_EN					FLTCONFIGbits.FLTBEN = 1;		
#define FLT_B_DIS					FLTCONFIGbits.FLTBEN = 0;

// FLTCONFIG<3>
#define FLT_AB_DEACT_ALL				FLTCONFIGbits.FLTCON = 1;	
#define FLT_AB_DEACT_0TO5				FLTCONFIGbits.FLTCON = 0;
#endif


// FLTCONFIG<1>
#define FLT_A_CY_CY					FLTCONFIGbits.FLTAMOD = 1;	
#define FLT_A_CATAS					FLTCONFIGbits.FLTAMOD = 0;

// FLTCONFIG<0>
#define FLT_A_EN					FLTCONFIGbits.FLTAEN = 1;	
#define FLT_A_DIS					FLTCONFIGbits.FLTAEN = 0;	


#if defined (PCPWM_V1) 

void Openpcpwm(unsigned char ,unsigned char ,unsigned char ,unsigned char,unsigned int ,unsigned int );

void Setdc0pcpwm(unsigned int );

void Setdc1pcpwm(unsigned int );

void Setdc2pcpwm(unsigned int );

void Closepcpwm(void);

void pcpwm_dt_clk_source(unsigned char );

void pcpwm_dt_assignment(unsigned char );

void pcpwm_OVD_CTRL(unsigned char );

void pcpwm_OVD_IO_STA(unsigned char );

#endif

#if defined (PCPWM_V2)

void Openpcpwm(unsigned char ,unsigned char ,unsigned char ,unsigned char,unsigned int ,unsigned int );

void Setdc0pcpwm(unsigned int );

void Setdc1pcpwm(unsigned int );

void Setdc2pcpwm(unsigned int );

void Setdc3pcpwm(unsigned int );

void Closepcpwm(void);

void pcpwm_dt_clk_source(unsigned char );

void pcpwm_dt_assignment(unsigned char );

void pcpwm_OVD_CTRL(unsigned char );

void pcpwm_OVD_IO_STA(unsigned char );

#endif


#if defined (PCPWM_V3) 

void Openpcpwm(unsigned char ,unsigned char ,unsigned char ,unsigned char,unsigned int ,unsigned int );

void Setdc0pcpwm(unsigned int );

void Setdc1pcpwm(unsigned int );

void Setdc2pcpwm(unsigned int );

void Closepcpwm(void);

void pcpwm_dt_clk_source(unsigned char );

void pcpwm_dt_assignment(unsigned char );

void pcpwm_OVD_CTRL(unsigned char );

void pcpwm_OVD_IO_STA(unsigned char );

#endif

#endif // __PCPWM_H

